<?php

namespace app\admin\controller;

use app\admin\model\Users as UsersModel;
use think\facade\Request;
use think\Log;

class Users extends Common
{
    //会员列表
    public function index()
    {
        if (request()->isPost()) {
            $key      = input('post.key');
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list     = db('users')->alias('u')
                ->join(config('database.prefix').'user_level ul', 'u.level = ul.level_id', 'left')
                ->field('u.*,ul.level_name')
                ->where('u.email|u.mobile|u.username', 'like', '%'.$key.'%')
                ->order('u.id desc')
                ->paginate(['list_rows' => $pageSize, 'page' => $page])
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                $list['data'][$k]['reg_time'] = date('Y-m-d H:s', $v['reg_time']);
            }

            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }

        return $this->fetch();
    }

    //设置会员状态
    public function usersState()
    {
        $id      = input('post.id');
        $is_lock = input('post.is_lock');
        if (false !== db('users')->where('id='.$id)->update(['is_lock' => $is_lock])) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }

    public function edit($id = '')
    {
        if (request()->isPost()) {
            $user             = db('users');
            $data             = Request::except('file');
            $level            = explode(':', $data['level']);
            $data['level']    = $level[1];
            $province         = explode(':', $data['province']);
            $data['province'] = isset($province[1]) ? $province[1] : '';
            $city             = explode(':', $data['city']);
            $data['city']     = isset($city[1]) ? $city[1] : '';
            $district         = explode(':', $data['district']);
            $data['district'] = isset($district[1]) ? $district[1] : '';
            if (empty($data['password'])) {
                unset($data['password']);
            } else {
                $data['password'] = md5($data['password']);
            }
            if (false !== $user->update($data)) {
                $result['msg']  = '会员修改成功!';
                $result['url']  = url('index');
                $result['code'] = 1;
            } else {
                $result['msg']  = '会员修改失败!';
                $result['code'] = 0;
            }

            return $result;
        } else {
            $province   = db('Region')->where(['pid' => 1])->select();
            $user_level = db('user_level')->order('sort')->select();
            $info       = UsersModel::get($id);
            $this->assign('info', json_encode($info, true));
            $this->assign('title', lang('edit').lang('user'));
            $this->assign('province', json_encode($province, true));
            $this->assign('user_level', json_encode($user_level, true));

            $city = db('Region')->where(['pid' => $info['province']])->select();
            $this->assign('city', json_encode($city, true));
            $district = db('Region')->where(['pid' => $info['city']])->select();
            $this->assign('district', json_encode($district, true));

            return $this->fetch();
        }
    }

    public function getRegion()
    {
        $Region     = db('region');
        $pid        = input('pid');
        $arr        = explode(':', $pid);
        $map['pid'] = $arr[1];
        $list       = $Region->where($map)->select();

        return $list;
    }

    public function usersDel()
    {
        db('users')->delete(['id' => input('id')]);
        db('oauth')->delete(['uid' => input('id')]);

        return $result = ['code' => 1, 'msg' => '删除成功!'];
    }

    public function delall()
    {
        $map[] = ['id', 'IN', input('param.ids/a')];
        db('users')->where($map)->delete();
        $result['msg']  = '删除成功！';
        $result['code'] = 1;
        $result['url']  = url('index');

        return $result;
    }

    /***********************************会员组***********************************/
    public function userGroup()
    {
        if (request()->isPost()) {
            $userLevel = db('user_level');
            $list      = $userLevel->order('sort')->select();

            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list, 'rel' => 1];
        }

        return $this->fetch();
    }

    public function groupAdd()
    {
        if (request()->isPost()) {
            $data = input('post.');
            db('user_level')->insert($data);
            $result['msg']  = '会员组添加成功!';
            $result['url']  = url('userGroup');
            $result['code'] = 1;

            return $result;
        } else {
            $this->assign('title', lang('add').'会员组');
            $this->assign('info', 'null');

            return $this->fetch('groupForm');
        }
    }

    public function groupEdit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            db('user_level')->update($data);
            $result['msg']  = '会员组修改成功!';
            $result['url']  = url('userGroup');
            $result['code'] = 1;

            return $result;
        } else {
            $map['level_id'] = input('param.level_id');
            $info            = db('user_level')->where($map)->find();
            $this->assign('title', lang('edit').'会员组');
            $this->assign('info', json_encode($info, true));

            return $this->fetch('groupForm');
        }
    }

    public function groupDel()
    {
        $level_id = input('level_id');
        if (empty($level_id)) {
            return ['code' => 0, 'msg' => '会员组ID不存在！'];
        }
        db('user_level')->where(['level_id' => $level_id])->delete();

        return ['code' => 1, 'msg' => '删除成功！'];
    }

    public function groupOrder()
    {
        $userLevel = db('user_level');
        $data      = input('post.');
        $userLevel->update($data);
        $result['msg']  = '排序更新成功!';
        $result['url']  = url('userGroup');
        $result['code'] = 1;

        return $result;
    }

    public function add()
    {
        if (request()->isPost()) {

            $user             = db('users');
            $data             = Request::except('file');
            $level            = explode(':', $data['level']);
            $data['level']    = $level[1];
            $province         = explode(':', $data['province']);
            $data['province'] = isset($province[1]) ? $province[1] : '';
            $city             = explode(':', $data['city']);
            $data['city']     = isset($city[1]) ? $city[1] : '';
            $district         = explode(':', $data['district']);
            $data['district'] = isset($district[1]) ? $district[1] : '';
            $data['reg_time'] = time();
            if (empty($data['password'])) {
                unset($data['password']);
            } else {
                $data['password'] = md5($data['password']);
            }
            if (false !== $user->insert($data)) {
                $result['msg']  = '会员添加成功!';
                $result['url']  = url('index');
                $result['code'] = 1;
            } else {
                $result['msg']  = '会员添加失败!';
                $result['code'] = 0;
            }

            return $result;
        } else{
            $province   = db('Region')->where(['pid' => 1])->select();
            $user_level = db('user_level')->order('sort')->select();

            $this->assign('title', lang('add').lang('user'));
            $this->assign('province', json_encode($province, true));
            $this->assign('user_level', json_encode($user_level, true));


            return $this->fetch();
        }


    }

    public function years(){
        if (request()->isPost()) {
            $key      = input('post.key');
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list     = db('user_year')
                ->order('year desc')
                ->paginate(['list_rows' => $pageSize, 'page' => $page])
                ->toArray();
          

            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }

        return $this->fetch();     
    }

    public function addYear(){
      
        if (request()->isPost()) {
          
            $insert = [
                'year'       => input('year'),
                'description' => input('description'),
            ];
            $m=db('user_year');
            $m->insert($insert);
    
            $result['msg']  = '添加成功!';
            $result['code'] = 1;

            return $result;
        } else{
            $this->assign('title',lang('add').lang('year'));
            $this->assign('info', 'null');
            return $this->fetch('edit_year'); 
        }
         
    }

    public function editYear(){
        if (request()->isPost()) {
            $data = Request::except('file');
          
            if (false !== db('user_year')->update($data)) {
                return ['code' => 1, 'url' => url('years'), 'msg' => '修改成功!'];
            } else {
                return ['code' => 0, 'url' => url('years'), 'msg' => '修改失败!'];
            }
        } else {
            $map['id'] = input('param.id');
            $info      = db('user_year')->where($map)->find();
            $this->assign('title', lang('edit').lang('year'));
            $this->assign('info', json_encode($info, true));

            return $this->fetch('edit_year');
        }
      
    }

    public function detail(){
        if (request()->isPost()) {
            $model             = db('setting');
            $value = input('post.value');
            $source=$model->where(['key'=>'user_detail'])->select();
            if($source){
               $r= $model->where(['key'=>'user_detail'])->update(['value'=>$value]);
            }
            else{
                $r=$model->insert(['key'=>'user_detail','value'=>$value]);
            }
            if (false !== $r) {
                $result['msg']  = '修改成功!';
                $result['url']  = url('detail');
                $result['code'] = 1;
            } else {
                $result['msg']  = '修改失败!';
                $result['code'] = 0;
            }

            return $result;
        } else {
            $model             = db('setting');
            $source=$model->where(['key'=>'user_detail'])->find();
          
            $this->assign('info', json_encode($source, true));

            return $this->fetch();
        }
    }

    public function yearDel(){
        db('user_year')->delete(['id' => input('id')]);
       

        return $result = ['code' => 1, 'msg' => '删除成功!'];
    }

    public function delYearAll(){
        $map[] = ['id', 'IN', input('param.ids/a')];
        db('user_year')->where($map)->delete();
        $result['msg']  = '删除成功！';
        $result['code'] = 1;
        $result['url']  = url('years');

        return $result;
    }
}
